package com.hurl.waguji.web.cx;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.base.dao.HibernateService;
import com.hurl.waguji.persistence.SelectOptionsBO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SelectorAction extends ActionSupport{
	private List<SelectOptionsBO> options = new ArrayList<SelectOptionsBO>();
	private Map<String,Object> queryResults = new HashMap<String,Object>();
	private String success;
	
	public String getSuccess(){
		return success;
	}
	public Map<String,Object> getQueryResults(){
		return this.queryResults;
	}
	public List<SelectOptionsBO> getOptions() {
		return options;
	}

	public String show()throws Exception{
		HibernateDao dao = HibernateService.getDao();
		options = (List<SelectOptionsBO>)dao.createCriteria(SelectOptionsBO.class).list();
		return SUCCESS;
	}
	
	public String query()throws Exception{
		List<String> select = new ArrayList<String>();
		select.add("a.gpdm");
		select.add("gpmc");
		List<String> where = new ArrayList<String>();
		List<String> order = new ArrayList<String>();
		List<SelectOptionsBO> optlist = new ArrayList<SelectOptionsBO>();
		
		HibernateDao dao = HibernateService.getDao();
		Map<String,Object> map = ActionContext.getContext().getParameters();
		Iterator<String> it = map.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			if(!key.startsWith("item_"))
				continue;
			String col = key.substring(5);
			SelectOptionsBO opt = dao.get(SelectOptionsBO.class, col);
			select.add(col);
			optlist.add(opt);
			String[] orderparam = (String[])map.get("order_"+key);
			if(orderparam!=null && StringUtils.isNotBlank(orderparam[0])){
				order.add(col+" "+orderparam[0]);
			}
			
			String[] value = (String[])map.get(key);
			if(value.length==0)
				continue;
			
			if(opt.getFwlx().equals("enum")){
				if(StringUtils.isBlank(value[0])){
					continue;
				}
				
				if(opt.getSjlx().equals("number")){
					String wherestr = col + " in (";
					for(int i=0; i<value.length;i++){
						if(StringUtils.isBlank(value[i]))
							value[i]="0";
						if(i==0){
							wherestr+=value[i];
						}else{
							wherestr+=","+value[i];
						}
					}
					wherestr+=")";
					where.add(wherestr);
					continue;
				}

				String wherestr = col + " in (";
				for(int i=0; i<value.length;i++){
					if(i==0){
						wherestr+="'"+value[i]+"'";
					}else{
						wherestr+=",'"+value[i]+"'";
					}
				}
				wherestr+=")";
				where.add(wherestr);
				continue;
			}
			
			if(opt.getFwlx().equals("range")){
				if(value.length!=2)
					continue;
				
				if(opt.getSjlx().equals("number")){
					if(StringUtils.isBlank(value[0]))
						value[0]="0";
					if(StringUtils.isBlank(value[1]))
						value[1]="0";
					String wherestr=col+" between "+value[0]+" and "+value[1];
					where.add(wherestr);
					continue;
				}

				String wherestr=col+" between '"+value[0]+"' and '"+value[1]+"'";
				where.add(wherestr);
				continue;
			}
		}
		
		String[] start = (String[])map.get("start");
		String[] limit = (String[])map.get("limit");
		int startint = 0;
		if(start!=null && StringUtils.isNotBlank(start[0])){
			startint = Integer.parseInt(start[0]);
		}
		int limitint = 20;
		if(limit!=null && StringUtils.isNotBlank(limit[0])){
			limitint = Integer.parseInt(limit[0]);
		}
		String limitstr = "limit "+startint+","+limitint;
		
		List<Map<String,Object>> root = new ArrayList<Map<String,Object>>();
		String sql = buildSQL(select,where,order,limitstr);
		List<Object[]> list = (List<Object[]>)dao.queryBySQL(sql).list();
		if(list!=null && !list.isEmpty()){
			for(int i=0;i<list.size();i++){
				Map<String,Object> rowmap = new HashMap<String,Object>();
				for(int j=0;j<list.get(i).length;j++){
					Object content = list.get(i)[j];
					if(j>=2){
						SelectOptionsBO opt = optlist.get(j-2);
						if(StringUtils.equals(opt.getSjlx(),"number") && StringUtils.isNotBlank(opt.getFormat())){
							content = StringUtils.formatNumber(content, opt.getFormat());
						}
					}
					rowmap.put(select.get(j).equals("a.gpdm")?"gpdm":select.get(j), content);
				}
				root.add(rowmap);
			}
		}
		queryResults.put("root", root);
		
		int totalProperty = 0;
		String countsql = buildCountSQL(where);
		totalProperty = Integer.parseInt(String.valueOf(dao.queryBySQL(countsql).list().get(0)));
		queryResults.put("totalProperty", totalProperty);
		return SUCCESS;
	}
	private String buildCountSQL(List<String> where){
		List<String> list = new ArrayList<String>(1);
		list.add("count(*)");
		return buildSQL(list,where,new ArrayList<String>(0),"");
	}
	private String buildSQL(List<String> select, List<String> where,List<String> order, String limit){
		String selectstr = StringUtils.splitList(select,",");
		String wherestr = null;
		if(where.isEmpty()){
			wherestr = "1=1";
		}else{
			wherestr = StringUtils.splitList(where, " and ");
		}
		String orderstr = null;
		if(order.isEmpty()){
			orderstr="";
		}else{
			orderstr = "order by "+StringUtils.splitList(order,",");
		}
		
		return "select "+selectstr+" from sj_profile a, sj_profile_cw b where a.gpdm=b.gpdm and "+wherestr+" "+orderstr+" "+limit;
		
	}
}
